package com.dwj.auto.comment.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.dwj.auto.comment.common.constant.RedisKey;
import com.dwj.auto.comment.common.utils.RedisCacheUtil;
import com.dwj.auto.comment.config.mybatis.plus.CommonServiceImpl;
import com.dwj.auto.comment.dao.entity.Menu;
import com.dwj.auto.comment.dao.mapper.MenuMapper;
import com.dwj.auto.comment.model.vo.ClearInfo;
import com.dwj.auto.comment.model.vo.HomeInfo;
import com.dwj.auto.comment.model.vo.LogoInfo;
import com.dwj.auto.comment.model.vo.MenuInfoVo;
import com.dwj.auto.comment.model.vo.MenuInitVo;
import com.dwj.auto.comment.model.vo.MenuVo;
import com.dwj.auto.comment.model.vo.UserVo;
import com.dwj.auto.comment.service.IMenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dwjian
 * @since 2020-09-18
 */
@Service
public class MenuServiceImpl extends CommonServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public MenuInitVo initMenu(UserVo currentUser, List<String> authorities) {
        MenuInitVo menuVoListCache = RedisCacheUtil.get(RedisKey.MENU.MENU.concat(currentUser.getId()), MenuInitVo.class);
        if(menuVoListCache != null){
            return menuVoListCache;
        }else {
            MenuInitVo menuInitVo = new MenuInitVo();
            menuInitVo.setClearInfo(new ClearInfo("/api/clear.json"));
            menuInitVo.setHomeInfo(new HomeInfo("首页", "/page/auto-comment/auto-comment-step.html"));
            menuInitVo.setLogoInfo(new LogoInfo("自动评语", "images/logo.png" ,"/index.html"));
            //过滤权限菜单
            List<MenuVo> menuVoList = recursiveQueryMenu(authorities, null, 0);
            if(!CollectionUtils.isEmpty(menuVoList)){
                MenuInfoVo menuInfo = new MenuInfoVo();
                for (MenuVo menuVo : menuVoList) {
                    if("常规管理".equals(menuVo.getTitle())){
                        menuInfo.setCurrency(menuVo);
                    }else if("组件管理".equals(menuVo.getTitle())){
                        menuInfo.setComponent(menuVo);
                    }else if("其它管理".equals(menuVo.getTitle())){
                        menuInfo.setOther(menuVo);
                    }
                }
                menuInitVo.setMenuInfo(menuInfo);
                RedisCacheUtil.set(RedisKey.MENU.MENU.concat(currentUser.getId()), menuInitVo, 24L, TimeUnit.HOURS);
                return menuInitVo;
            }
            return new MenuInitVo();
        }
    }

    private List<MenuVo> recursiveQueryMenu(List<String> authorities, Long parentId, Integer level) {
        List<Menu> menuList = getMenus(authorities, parentId, level);
        if (CollectionUtil.isNotEmpty(menuList)) {
            List<MenuVo> menuVoList = new ArrayList<>();
            for (Menu menu : menuList) {
                MenuVo menuVo = new MenuVo();
                menuVo.setTitle(menu.getMenuName());
                menuVo.setIcon(menu.getMenuIcon());
                menuVo.setHref(menu.getMenuUrl());
                menuVo.setTarget(menu.getTarget());
                menuVo.setAuthority(menu.getAuthority());
                List<MenuVo> child = recursiveQueryMenu( authorities, menu.getId(), menu.getLevel() + 1);
                menuVo.setChild(child);
                menuVoList.add(menuVo);
            }
            return menuVoList;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    private List<Menu> getMenus(List<String> authorities, Long parentId, Integer level) {
        //查询导航菜单
        return lambdaQuery().eq(Menu::getLevel, level)
                .eq(parentId != null, Menu::getParentId, parentId)
                .eq(Menu::getIsMenu, 1).eq(Menu::getIsDel, 0)
                .and(n -> n.isNull(Menu::getAuthority)
                        .or(CollectionUtil.isNotEmpty(authorities), i -> i.in(Menu::getAuthority,authorities))
                        .or().eq(Menu::getAuthority,""))
                .orderByAsc(Menu::getSort,Menu::getCreateDate).list();
    }
}
